<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>
      <span>·</span>
      <a href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="user">
        <!--手机-->
        <el-form-item v-if="isMobile" class="input-prepend restyle" prop="mobile"
                      :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' },{validator: checkPhone, trigger: 'blur'}]">
          <div>
            <el-input type="text" placeholder="手机号" v-model="user.mobile"/>
            <i class="iconfont icon-phone"/>
          </div>
        </el-form-item>
        <!--邮箱-->
        <el-form-item v-else class="input-prepend restyle" prop="email"
                      :rules="[{ required: true, message: '请输入邮箱', trigger: 'blur' }
                      ,
                      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }]">
          <div>
            <el-autocomplete
              style="width: 300px"
              v-model="user.email"
              :fetch-suggestions="querySearch"
              placeholder="请输入内容"
              :trigger-on-focus="false"
              @select="handleSelect"
            >
              <template slot-scope="{ item }">
                <span class="addr">{{ item }}</span>
              </template>
            </el-autocomplete>
            <i class="iconfont icon-mail"/>
          </div>
        </el-form-item>

        <el-form-item class="input-prepend" prop="password"
                      :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
          <div>
            <el-input type="password" placeholder="密码" v-model="user.password"/>
            <i class="iconfont icon-password"/>
          </div>
        </el-form-item>

        <div class="btn">
          <input type="button" class="sign-in-button" value="登录" @click="submitLogin()">
        </div>
      </el-form>
      <!-- 更多登录方式 -->
      <div class="more-sign">
        <h6>社交帐号登录</h6>
        <ul>
          <li><a id="weixin" class="weixin" target="_blank"
                 href="http://114.55.238.50:8160/api/ucenter/wx/login"><i class="iconfont icon-weixin"/></a>
          </li>
          <li><a id="qq" class="qq" target="_blank" href="#"><i class="iconfont icon-qq"/></a></li>
        </ul>
      </div>
    </div>

  </div>
</template>

<script>
  import '~/assets/css/sign.css'
  import '~/assets/css/iconfont.css'
  import cookie from 'js-cookie'
  import login from "../api/login";

  export default {
    layout: 'sign',

    data() {
      return {
        user: {
          mobile: '',
          password: '',
          email: '',
          regType: this.isMobile ? 'mobile' : 'email' //验证方式 后端防止篡改
        },
        loginInfo: {},
        isMobile: false //是否手机注册 否为邮箱
      }
    },

    methods: {
      //登录的方法
      submitLogin() {
        //符合规则才让执行
        this.$refs['userForm'].validate((valid) => {
          if (valid) {
            //第一步 调用接口进行登录，返回token字符串
            login.submitLoginUser(this.user)
              .then(resp => {
                if (resp.data.code === 20001) {
                  this.$message({
                    type: 'error',
                    message: resp.data.message
                  })
                } else {
                  //第二步 获取token字符串放到cookie里面
                  // 第一个参数：cookie名称,第二个参数：参数值,第三个参数：作用范围 localhost：本地,如果用ip的话写ip地址
                  cookie.set('edu_token', resp.data.data.token)
                  //第四步 调用接口，根据token获取用户信息,为了首页面显示
                  login.getLoginUserInfo()
                    .then(resp => {
                      console.log("pppppp:" + resp.data.data.userInfo)
                      this.loginInfo = JSON.stringify(resp.data.data.userInfo);
                      console.log("wwwwwww:" + this.loginInfo)
                      //获取用户信息，放在cookie中
                      cookie.set('edu_ucenter', this.loginInfo);
                      //回到首页面中
                      //this.$router.push({path: '/'})
                      //回到上一个页面
                      this.$router.go(-1);
                    })
                }

              })
          }
        })
      },

      handleSelect(item) {
        if (!(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(item))) {
          return new Error('邮箱格式不正确')
        }
        this.user.email = item
      },
      //输入建议
      querySearch(queryString, cb) {
        const emailsuffix = ["@qq.com", "@163.com", "@gmail.com", "@yahoo.com"]
        let results = [];
        if ((queryString.split('@')).length - 1 === 0) {//判断@符号没出现 就添加邮箱建议
          for (let i = 0; i < emailsuffix.length; i++) {
            results[i] = queryString + emailsuffix[i];
          }
        }
        cb(results);
      },

    }
  }
</script>
<style>
  .el-form-item__error {
    z-index: 9999999;
  }
</style>
